using AutoMapper;
using B.S.BaseData.Api.Write.Applicantion.Command.HospitalTwoCommand;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Hosp;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.HospitalTwoCommandHandler
{
    public class CarouselImageUpdateCommandHandler : IRequestHandler<CarouselImageUpdateCommand, APIResult<int>>
    {
        private readonly IBaseRepository<CarouselImage> _repository;
        private readonly IMapper _mapper;
        public CarouselImageUpdateCommandHandler(IBaseRepository<CarouselImage> repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(CarouselImageUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new APIResult<int>();

            try
            {
                var list= _mapper.Map<CarouselImage>(request);
                result.Data = await _repository.UpdateAsync(list);
                result.Code = ApiEnum.Success;
                result.Msg = "轮播图更新成功";
                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {
                result.Code = ApiEnum.Fail;
                result.Msg = $"更新轮播图失败：{ex.Message}";
                return await Task.FromResult(result);
            }
        }
    }
} 